package cn.edu.zut.cs.Filters;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class LoginFilter implements HandlerInterceptor {



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      //获取请求链接
       String url = request.getRequestURI();
       String path=request.getContextPath();
       System.out.println(url);
       //登录 注册 找回密码不拦截
        if(url.indexOf("/jsp/login.jsp")>=0||url.indexOf("/toLogin")>=0||url.indexOf("/register")>=0||url.indexOf("/forgetpwd")>=0||url.indexOf("/reset")>=0){
            return true;
        }
        HttpSession session=request.getSession();
        Object object=session.getAttribute("userInfo");
        if (object!=null){
            return true;
        }
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}
